
function debounce(fn, delay) {
	var timer;
	return function(e) {
		timer && clearTimeout(timer);
		timer = setTimeout(() => {
			fn(this, e)
		}, delay);
	}
}

function throttle(fn, delay) {
	var valid = true;
	return function(e) {
		if (!valid) {
			return;
		}
		valid = false;
		setTimeout(() => {
			valid = true;
			fn(this, e);
		}, delay);
	}
}

function hue2RGB(h) {
	let doHandle = (num) =>{
			if( num > 255) {
					return 255;
			} else if(num < 0){
					return 0;
			} else {
					return Math.round(num);
			}
	}

	let hueRGB = h/60 * 255;
	let r = doHandle(Math.abs(hueRGB-765)-255);
	let g = doHandle(510 - Math.abs(hueRGB-510));
	let b = doHandle(510 - Math.abs(hueRGB-1020));
	return 'rgb(' +r + ',' + g + ',' + b + ')';  
}

function rgb2Hex (color){
	if(color.indexOf("#") != -1) {
			return color;
	}
	let arr = color.split(',');
	let r = +arr[0].split('(')[1];
	let g = +arr[1];
	let b = +arr[2].split(')')[0];
	let value = (1 << 24) + r * (1 << 16) + g * (1 << 8) + b;
	value = value.toString(16);
	return '#' + value.slice(1);
}

module.exports = {
  debounce,
	throttle,
	hue2RGB,
	rgb2Hex
}
